const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    member_voucher_id:"",
    max_price:"",
    min_price:"",
    value: "",
    title:"",
    tabs: ['全部', '未寄售', '寄售中', '已出售', '自行寄回'],
    active: 0,
    text: "",
    list: [],
    is_custom:1,
    show: false,

    page: 1,
    total_page: 0,
    showDescription: false,
    agreement: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.setData({
      show:false,
      active:0,
      member_voucher_id:'',
    })
    this.getData()
  },


  handle_conset(e){
    let item = e.currentTarget.dataset.item;
    app._postNetWork({
      url:app.getNetAddresss('plugin.consignment.frontend.controllers.member-exchange-voucher.is-agree'),
      data:{member_voucher_id:item.id},
      success:resdata=>{
        let res = resdata.data;
        app.tips(res.msg)
        if(res.result){
          this.getData(1)
        }
      }
    })

  },

  handle_repeal(e){
    wx.showModal({
      title: '提示',
      content: '是否确定撤销寄售',
      success: (resd)=> {
        if (resd.confirm) {
          let item = e.currentTarget.dataset.item;
          app._postNetWork({
            url:app.getNetAddresss('plugin.consignment.frontend.controllers.member-exchange-voucher.quash-sold'),
            data:{member_voucher_id:item.id},
            success:resdata=>{
              let res = resdata.data;
              app.tips(res.msg)
              if(res.result){
                this.getData(1)
              }
            }
          })
        }
      }
    })
    

  },
  input_handle(e){
    this.setData({value:e.detail})
  },
  confirm(){
    app._postNetWork({
      url:app.getNetAddresss('plugin.consignment.frontend.controllers.member-exchange-voucher.sold'),
      data:{
        member_voucher_id:this.data.member_voucher_id,
        price:this.data.value
      },
      success:resdata=>{
        let res = resdata.data;
        app.tips(res.msg)
        if(res.result){
          this.setData({
            showDescription: false
          })
          this.getData(1)
        }
      }
    })

  },
  check_orders(e){
    let  item = e.currentTarget.dataset.item;
    let urlStr = `/packageA/member/orderdetail/orderdetail?order_id=${item.sold_order_id}&orderType=shop&show_detail_pay=0`
    wx.navigateTo({
      url: urlStr,
    })
  },
  show_dialog(e) {
    let {exchange_voucher:{max_price,min_price,is_custom},can_sold,id,price} = e.currentTarget.dataset.item;
    if(can_sold){
      if(is_custom==1){
        this.setData({value:"",title:"寄售设置"})
      }else{
        this.setData({value:"",title:"是否确认寄售商品"})

      }
      this.setData({
        showDescription:true,
        value:price,
        is_custom,
        member_voucher_id:id,
        max_price,
        min_price
      })
    }
  },
  change(e) {
    this.setData({active:e.detail.index})
    this.getData(1)
  },
  getData(page = 1) {
    let json = {
      page,
      status:this.data.active - 1,
      validate_page: 1
    }
    if(this.data.active==0){
      delete json.status
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.consignment.frontend.controllers.member-exchange-voucher.index"),
      data:json,
      success:resdata=>{
        let {result,data,msg,validate_page} = resdata.data;
        if(result){
          let {vember_voucher_list,plugin_name,agreement} = resdata.data.data;
          let _list = [];
          (page==1)?(_list = vember_voucher_list.data || []):(_list= this.data.list.concat(vember_voucher_list.data));
          this.data.page = vember_voucher_list.current_page;
          this.data.total_page = vember_voucher_list.last_page;
          this.setData({
            list:_list,
            agreement: agreement
          })
          if(validate_page.is_auth == 0 && validate_page.real_name_auth_is_open && validate_page.real_name_auth_scene.indexOf("160") > -1 ){
            this.isAuth = true;//需要实名
          }
          wx.setNavigationBarTitle({
            title: plugin_name,
          })
        }else{
          app.tips(msg)
        }
      }
    })
  },
  getMoreData() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getData(page)
  },

  toAuth(){
    if( !this.isAuth ){
      this.setData({
        show: true
      })
      return;
    };
    // 去实名认证
    wx.setStorageSync("real_name_redirect", 'packageI/exchange_bond/member/member');
    wx.reLaunch({
      url: "/packageH/signPage/personBank/personBank?from=bindRealName",
    });
  },
  toOrders(e) {
    let item = e.currentTarget.dataset.item;
    let url = "/packageD/buy/myOrder_v2/myOrder_v2?goodsId=" + item.goods_id + "&optionsId=0&total=1&tag=consignment";
    wx.navigateTo({
      url: url + "&VoID=" + item.id,
    });         
  },

  onCloseDescription(){
    this.setData({
      showDescription: false
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})